<?php
/**
 * Created by PhpStorm.
 * User: fengbob
 * Date: 2018/2/15
 * Time: 22:08
 */

namespace app\controllers;


use app\librarys\ExpressQuery;
use app\librarys\PayProController;
use app\librarys\Tools;
use app\models\db\Express;
use app\models\db\Order;
use app\models\OrderExt;
use yii\data\Pagination;
use yii\db\Exception;

class OrderController extends PayProController
{
    public $enableCsrfValidation = false;


    /**
     *
     * @return string
     */
    public function actionOrder()
    {
//        debugger;
//        var_dump('in order action');
        $request = \Yii::$app->request;

        $name = $request->get('name');

        $count = OrderExt::getOrderCount($name);
        $pagination = new Pagination([
            'totalCount' => $count
        ]);
        $items = OrderExt::getOrderList($pagination->page, $pagination->pageSize, $name);

        return $this->render('order',
            [
                'items' => $items,
                'name' => $name,
                'pagination' => $pagination
            ]);
    }

    public function actionOrderPartial()
    {
        $this->layout = false;
        $request = \Yii::$app->request;
        $id = $request->post('id');

        $item = OrderExt::getOrderById($id);
        if(!empty($item))
        $expresses = Express::findAll(['order_id'=> $item->id]);
//        $item = '';
        return $this->render('order-partial',
            [
                'item' => $item,
                'expresses' => $expresses
            ]
        );
    }

//    public function actionSaveExpress(){
//        $request = \Yii::$app->request;
//        $express_no = $request->post('express_no');
//        $order_id = $request->post('order_id');
////        \app\librarys\ExpressQuery::query($express_no);
//        $express = new Express();
//        $express->id = Tools::db_uuid();
//        $express->express_no = $express_no;
//        $express->order_id = $order_id;
//        $result = $express->save();
////        $result = ExpressQuery::query($express_no);
//        if($result)
//            return $this->renderJson($result, 0, '操作成功');
//        else
//            return $this->renderJson($result, 100, '保存失败，请确认快递单填写正确');
//    }

    /**
     * 快递查询
     * @return mixed
     */
    public function actionExpressQuery(){
        $request = \Yii::$app->request;
        $express_no = $request->post('express_no');
//        \app\librarys\ExpressQuery::query($express_no);
        $result = ExpressQuery::query($express_no);
        return $result;
    }


    /**
     * 发货
     */
    public function actionSaveExpress(){
        $this->layout = false;
        $request = \Yii::$app->request;
         $order_id = $request->post('order_id');
         $express_no = $request->post('express_no');
         $express_comanpy = $request->post('express_comanpy','auto');

//         var_dump($express_no);

         //先删除所有本订单快递
        $olds = Express::findAll(['order_id'=>$order_id]);

//      $tran = Express::getDb()->beginTransaction();
//        try {
//      $tran->begin();
      if (!empty($olds)) {
          foreach ($olds as $old) {
//              echo $old->express_no;
              $old->delete();
          }
      }
      $result = 0;

        $temps = explode(chr(10),$express_no);
        foreach ($temps as $e){
//            var_dump($e);
            $express = new Express();
            $express->id = Tools::db_uuid();
            $express->express_no = $e;
            $express->order_id = $order_id;
            $express->express_comanpy = $express_comanpy;
            $result = $express->save();
        }

        if($result>0){
            $order = Order::findOne($order_id);
            $order->status = 22;
            $order->save();
        }

//      $tran->commit();
//  }catch (Exception $e){
//            var_dump($e);
//      $tran->rollBack();
//      $result = -1;
//  }
//  var_dump('ok');
  return '成功保存';

    }

//    public function actionOrderSaveHandler()
//    {
//        $request = \Yii::$app->request;
//        $id = $request->post('id');
//        $name = $request->post('name');
//        $raw_id = $request->post('raw_id');
//        $app_id = $request->post('app_id');
//        $app_secret = $request->post('app_secret');
//        $type = $request->post('type');
//        $begin_datetime = $request->post('begin_datetime');
//        $end_datetime = $request->post('end_datetime');
//        $is_enabled = $request->post('is_enabled');
//
////        $result = OrderExt::saveMiniProgram($id, $name, $raw_id, $app_id, $app_secret, $type, $begin_datetime, $end_datetime, $is_enabled);
////        return $this->renderJson(null, $result ? 0 : 1, $result ? '操作成功' : '操作失败');
//    }

    public function actionOrderDeleteHandler()
    {
        $request = \Yii::$app->request;
        $id = $request->post('id');

        $result = OrderExt::deleteOrder($id);
        return $this->renderJson(null, $result ? 0 : 1, $result ? '操作成功' : '操作失败');
    }
}